﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FastColoredTextBoxNS;

namespace FAP.GUI.UC
{
    public partial class ucConsole : UserControl
    {
        static TextStyle fatalStyle = new TextStyle(Brushes.Red, null, FontStyle.Regular);
        static TextStyle warningStyle = new TextStyle(Brushes.Purple, null, FontStyle.Regular);
        static TextStyle errorStyle = new TextStyle(Brushes.Red, null, FontStyle.Regular);
        static TextStyle infoStyle = new TextStyle(Brushes.Yellow, null, FontStyle.Regular);
        static TextStyle debugStyle = new TextStyle(Brushes.White, null, FontStyle.Regular);

        public ucConsole()
        {
            InitializeComponent();
        }

        public void AddText(String text, int type)
        {
            switch (type)
            {
                case 1:
                    Log("[" + DateTime.Now + "]" + text, infoStyle); break;
                case 2:
                    Log("[" + DateTime.Now + "]" + text, debugStyle); break;
                case 3:
                    Log("[" + DateTime.Now + "]" + text, warningStyle); break;
                case 4:
                    Log("[" + DateTime.Now + "]" + text, errorStyle); break;
                case 5:
                    Log("[" + DateTime.Now + "]" + text, fatalStyle); break;

            }
        }

        private void Log(string text, Style style)
        {
            txtLogger.BeginUpdate();
            txtLogger.Selection.BeginUpdate();
            //remember user selection
            var userSelection = txtLogger.Selection.Clone();
            //add text with predefined style
            txtLogger.AppendText(text + Environment.NewLine, style);
            //restore user selection
            if (!userSelection.IsEmpty || userSelection.Start.iLine < txtLogger.LinesCount - 2)
            {
                txtLogger.Selection.Start = userSelection.Start;
                txtLogger.Selection.End = userSelection.End;
            }
            else
                txtLogger.GoEnd();//scroll to end of the text
            //
            txtLogger.Selection.EndUpdate();
            txtLogger.EndUpdate();
        }

        private void btnGoEnd_Click(object sender, EventArgs e)
        {
            txtLogger.GoEnd();
        }
    }
}
